﻿using System;
using System.Windows.Forms;
using TSites.GUI.Enums;
using TSites.GUI.Properties;
using VNMessageBox;
using TSites.GUI.Languages;
using System.Globalization;
using System.Resources;
using System.Diagnostics;

namespace TSites.GUI
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private Form _childForm = null;
		private CHILD_FORM _childEnum = CHILD_FORM.None;

		#region Methods

		private void ApplyResource()
		{
			CultureInfo culture = CultureInfoFactory.GetInstance();
			ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmMain).Assembly);

			this.Text = rm.GetString("frmMain", culture) + " v." + "1.0";

			tsCompany.Text = rm.GetString("tsCompany", culture);
			tsImmanuelInfo.Text = rm.GetString("tsImmanuel", culture);
			tsLanguage.Text = rm.GetString("Languages", culture);
			tsVietnames.Text = rm.GetString("Vietnames", culture);
			tsEnglish.Text = rm.GetString("English", culture);
			tsAbout.Text = rm.GetString("About", culture);
			tsHelp.Text = rm.GetString("UserManual", culture);
			tsClose.Text = rm.GetString("Close", culture);
		}

		private void OpenChildForm(CHILD_FORM childEnum)
		{
			if (_childEnum != childEnum)
			{
				_childEnum = childEnum;

				// close first
				if (_childForm != null)
				{
					_childForm.Close();
					_childForm = null;
				}

				try
				{
					_childForm = ChildFormFactory.Instance(_childEnum);
					//set parent form for the child window
					_childForm.MdiParent = this;
					_childForm.Dock = DockStyle.Fill;

					//display the child window
					_childForm.Show();
				}
				catch (Exception ex)
				{
					LANGUAGE language = ConfigurationSingleton.Instance.Language;
					CultureInfo culture = CultureInfoFactory.GetInstance();
					ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

					string title = rm.GetString("Error", culture);
					if (language == LANGUAGE.Vietnames)
					{
						CMessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					else
					{
						MessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}
		
		#endregion

		#region Events
		
		private void frmMain_Load(object sender, EventArgs e)
		{
			// Get configuration
			ConfigurationSingleton config = ConfigurationSingleton.Instance;
			if (config.Language == LANGUAGE.Vietnames)
			{
				tsVietnames.Enabled = false;
			}
			else
			{
				tsEnglish.Enabled = false;
			}

			OpenChildForm(CHILD_FORM.Introduction);
			ApplyResource();

			// Init Company hash
			CompanySingleton companySingle = CompanySingleton.Instance;
		}
		
		private void tsExport_Click(object sender, EventArgs e)
		{
			OpenChildForm(CHILD_FORM.Export);
		}

		private void tsCompany_Click(object sender, EventArgs e)
		{
			OpenChildForm(CHILD_FORM.Company);
		}

		private void tsImmanuelInfo_Click(object sender, EventArgs e)
		{
			OpenChildForm(CHILD_FORM.ImmanuelInfo);
		}

		private void tsAbout_Click(object sender, EventArgs e)
		{
			OpenChildForm(CHILD_FORM.About);
		}

		private void tsClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void tsLanguages_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem tsLanguage = (ToolStripMenuItem)sender;

			LANGUAGE language;
			if (string.Equals(tsLanguage.Name, "tsVietnames", StringComparison.OrdinalIgnoreCase))
			{
				language = LANGUAGE.Vietnames;
				tsVietnames.Enabled = false;
				tsEnglish.Enabled = true;
			}
			else
			{
				language = LANGUAGE.English;
				tsVietnames.Enabled = true;
				tsEnglish.Enabled = false;
			}

			try
			{
				ConfigurationSingleton.Instance.SaveLanguage(language);
			}
			catch (Exception ex)
			{
				CultureInfo culture = CultureInfoFactory.GetInstance();
				ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

				string title = rm.GetString("Error", culture);
				if (language == LANGUAGE.Vietnames)
				{
					CMessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				else
				{
					MessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			if (_childForm != null)
			{
				if (_childForm is frmCompany)
				{
					((frmCompany)_childForm).ChangeLanguage();
				}
				else if (_childForm is frmImmanuelInfo)
				{
					((frmImmanuelInfo)_childForm).ApplyResource();
				}
			}

			// refresh GUI
			ApplyResource();
		}

		private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
		{
			LANGUAGE language = ConfigurationSingleton.Instance.Language;
			CultureInfo culture = CultureInfoFactory.GetInstance();
			ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmMain).Assembly);

			string message, title;
			message = rm.GetString("ConfirmCloseApp", culture);
			title = rm.GetString("Notice", culture);
			
			DialogResult dlgResult;
			if (language == LANGUAGE.Vietnames)
			{
				dlgResult = CMessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			}
			else
			{
				dlgResult = MessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			}

			if (dlgResult == DialogResult.No)
			{
				e.Cancel = true;
			}
		}

		#endregion

		private void tsHelp_Click(object sender, EventArgs e)
		{
			try
			{
				Process.Start(ConfigurationSingleton.Instance.UserManualPath);
			}
			catch (Exception) { }
		}
	}
}